package ip.command;
/***********************************************************************
 * Module:  UpdateCountryCommand.java
 * Author:  mojsilo
 * Purpose: Defines the Class UpdateCountryCommand
 ***********************************************************************/

import ip.dto.CountryDTO;
import ip.ejb.session.facade.CountryFacadeLocal;
import ip.ejb.session.facade.CountryFacadeLocalHome;
import ip.exceptions.ApplicationException;
import ip.exceptions.NoSuchCountryException;
import ip.exceptions.UnexpectedException;
import ip.util.EJBHomeFactory;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.RemoveException;
import javax.naming.NamingException;

import org.apache.log4j.Logger;

public class UpdateCountryCommand extends Command
{
	private CountryDTO countryDTO = null;
	private CountryFacadeLocal countryFacadeLocal;
	private Logger logger = Logger.getLogger(this.getClass());

	public void init() throws ApplicationException{
		try {
			logger.debug("init()");
			CountryFacadeLocalHome countryFacadeLocalHome = (CountryFacadeLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.session.facade.CountryFacadeLocalHome.class);
			countryFacadeLocal = countryFacadeLocalHome.create();

		} catch (NamingException e) {
			logger.error(e);
			e.printStackTrace();
			throw new UnexpectedException();			
		} catch (CreateException e) {
			logger.error(e);
			e.printStackTrace();			
			throw new UnexpectedException();			
		}

	}

	public void execute() throws ApplicationException{
		try {
			logger.debug("execute()");
			countryFacadeLocal.updateCountry(countryDTO);
		} catch (NoSuchCountryException e) {			
			e.printStackTrace();
			throw new NoSuchCountryException(countryDTO);			
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();			
			throw new UnexpectedException();			
		}
	}

	public void dispose() throws ApplicationException{
		try {
			logger.debug("dispose()");
			countryFacadeLocal.remove();

		} catch (EJBException e) {
			logger.error(e);
			e.printStackTrace();
			throw new UnexpectedException();
		} catch (RemoveException e) {
			logger.error(e);
			e.printStackTrace();
			throw new UnexpectedException();
		}
	}

	public void setCountryDTO(CountryDTO countryDTO)
	{
		this.countryDTO = countryDTO;
	}

}